package com.zis.system.server;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zis.starter.core.Constant;
import com.zis.starter.core.ResponseEntity;
import com.zis.starter.core.SearchParams;
import com.zis.system.dao.RegionDao;
import com.zis.system.inter.IRegionInter;
import com.zis.system.model.Region;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Create by wangshen 2018/5/28
 */
@Service
@Transactional("txManager")
public class RegionInterImpl implements IRegionInter {

    @Autowired
    private RegionDao regionDao;

    @Override
    public ResponseEntity insert(Region model) throws Exception {
        return null;
    }

    @Override
    public ResponseEntity update(Region model) throws Exception {
        return null;
    }

    @Override
    public ResponseEntity delete(String id) throws Exception {
        return null;
    }

    @Override
    public ResponseEntity selectById(String id) throws Exception {
        return null;
    }

    @Override
    public PageQuery<Region> findByPage(SearchParams params) throws Exception {
        return null;
    }

    @Override
    public ResponseEntity findByCondition(Map params) throws Exception {
        JSONArray dataList = new JSONArray();
        List<Region> regionList = regionDao.getSQLManager().select("system.region.findByCondition", Region.class, params);
        for (Region region : regionList) {
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("value", region.getRegionCode());
            jsonObj.put("label", region.getRegionName());
            dataList.add(jsonObj);
        }
        return ResponseEntity.success(dataList);
    }

    /**
     * 查询JSON树
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity findTreeJSON() throws Exception {
        JSONArray dataTree = new JSONArray();
        List<Region> rootList = regionDao.findByParentId("0");//查询根节点
        if(rootList != null && rootList.size() > 0) {
            for (Region region : rootList) {
                LinkedHashMap<String, Object> rootNode = new LinkedHashMap<>();
                rootNode.put("value", region.getRegionCode());
                rootNode.put("label", region.getRegionName());
                // 获取下级节点
                findBySubNode(region.getRegionId(), rootNode);
                dataTree.add(rootNode);
            }
            return ResponseEntity.success(dataTree);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_NULL);
    }


    private LinkedHashMap<String, Object> findBySubNode(String parentId, LinkedHashMap<String, Object> rootNode) {
        JSONArray subTree = new JSONArray();
        List<Region> subList = regionDao.findByParentId(parentId);
        if (subList != null && subList.size() > 0) {
            for (Region region : subList) {
                LinkedHashMap<String, Object> subNode = new LinkedHashMap<>();
                subNode.put("value", region.getRegionCode());
                subNode.put("label", region.getRegionName());
                List nodeList = regionDao.findByParentId(region.getRegionId());
                if (nodeList != null && nodeList.size() > 0) {
                    findBySubNode(region.getRegionId() ,subNode);
                }
                subTree.add(subNode);
            }
            rootNode.put("children", subTree);
            return rootNode;
        }
        return null;
    }
}
